﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Collections;

namespace DALL.Procurement
{
    public class T_PUB_PURCHASEMATERIAL
    {
        public T_PUB_PURCHASEMATERIAL()
        { }

        public void AddPurchase(Model.Procurement.T_PUB_PURCHASE model, IList<Model.Procurement.T_PUB_PURCHASEMATERIAL> addList)
        {
            ArrayList strArry = new ArrayList();    
            #region 添加采购单
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ID != null)
            {
                strSql1.Append("ID,");
                strSql2.Append("'" + model.ID + "',");
            }
            if (model.TITLE != null)
            {
                strSql1.Append("TITLE,");
                strSql2.Append("'" + model.TITLE + "',");
            }
            if (model.APPLICANT != null)
            {
                strSql1.Append("APPLICANT,");
                strSql2.Append("" + model.APPLICANT + ",");
            }
            if (model.APPLICANT_NAME != null)
            {
                strSql1.Append("APPLICANT_NAME,");
                strSql2.Append("'" + model.APPLICANT_NAME + "',");
            }
            if (model.STATE != null)
            {
                strSql1.Append("STATE,");
                strSql2.Append("'" + model.STATE + "',");
            }
            if (model.END_TIME != null)
            {
                strSql1.Append("END_TIME,");
                strSql2.Append("'" + model.END_TIME + "',");
            }
            if (model.REAL_TIME != null)
            {
                strSql1.Append("REAL_TIME,");
                strSql2.Append("'" + model.REAL_TIME + "',");
            }
            if (model.REMARK != null)
            {
                strSql1.Append("REMARK,");
                strSql2.Append("'" + model.REMARK + "',");
            }
            if (model.APPLICANT_TIME != null)
            {
                strSql1.Append("APPLICANT_TIME,");
                strSql2.Append("'" + model.APPLICANT_TIME + "',");
            }
            if (model.APPROVER != null)
            {
                strSql1.Append("APPROVER,");
                strSql2.Append("" + model.APPROVER + ",");
            }
            if (model.APPROVER_NAME != null)
            {
                strSql1.Append("APPROVER_NAME,");
                strSql2.Append("'" + model.APPROVER_NAME + "',");
            }
            if (model.ORDER_ID != null)
            {
                strSql1.Append("ORDER_ID,");
                strSql2.Append("" + model.ORDER_ID + ",");
            }
            if (model.BRANCH_ID != null)
            {
                strSql1.Append("BRANCH_ID,");
                strSql2.Append("" + model.BRANCH_ID + ",");
            }
            if (model.NOTATION != null)
            {
                strSql1.Append("NOTATION,");
                strSql2.Append("'" + model.NOTATION + "',");
            }
            strSql.Append("insert into T_PUB_PURCHASE(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strArry.Add(strSql);
            #endregion

            #region  添加采购材料
            foreach (Model.Procurement.T_PUB_PURCHASEMATERIAL item in addList)
            {
                StringBuilder strSqlm = new StringBuilder();
                StringBuilder strSqlm1 = new StringBuilder();
                StringBuilder strSqlm2 = new StringBuilder();
                if (item.PURCHASE_ID != null)
                {
                    strSqlm1.Append("PURCHASE_ID,");
                    strSqlm2.Append("'" + item.PURCHASE_ID + "',");
                }
                if (item.MATERIAL_ID != null)
                {
                    strSqlm1.Append("MATERIAL_ID,");
                    strSqlm2.Append("" + item.MATERIAL_ID + ",");
                }
                if (item.MATERIAL_NAME != null)
                {
                    strSqlm1.Append("MATERIAL_NAME,");
                    strSqlm2.Append("'" + item.MATERIAL_NAME + "',");
                }
                if (item.NUM != null)
                {
                    strSqlm1.Append("NUM,");
                    strSqlm2.Append("" + item.NUM + ",");
                }
                strSqlm.Append("insert into T_PUB_PURCHASEMATERIAL(");
                strSqlm.Append(strSqlm1.ToString().Remove(strSqlm1.Length - 1));
                strSqlm.Append(")");
                strSqlm.Append(" values (");
                strSqlm.Append(strSqlm2.ToString().Remove(strSqlm2.Length - 1));
                strSqlm.Append(")");
                strSqlm.Append(";select @@IDENTITY");
                strArry.Add(strSqlm);
            }
            #endregion

            DBHelper.ExecuteSqlTran(strArry);
        }


        public void EditPurchase(Model.Procurement.T_PUB_PURCHASE model, IList<Model.Procurement.T_PUB_PURCHASEMATERIAL> addList, IList<Model.Procurement.T_PUB_PURCHASEMATERIAL> editList)
        {
            ArrayList strArry = new ArrayList();
            #region 更新采购单
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_PURCHASE set ");
            if (model.TITLE != null)
            {
                strSql.Append("TITLE='" + model.TITLE + "',");
            }
            else
            {
                strSql.Append("TITLE= null ,");
            }
            if (model.APPLICANT != null)
            {
                strSql.Append("APPLICANT=" + model.APPLICANT + ",");
            }
            else
            {
                strSql.Append("APPLICANT= null ,");
            }
            if (model.APPLICANT_NAME != null)
            {
                strSql.Append("APPLICANT_NAME='" + model.APPLICANT_NAME + "',");
            }
            else
            {
                strSql.Append("APPLICANT_NAME= null ,");
            }
            if (model.STATE != null)
            {
                strSql.Append("STATE='" + model.STATE + "',");
            }
            else
            {
                strSql.Append("STATE= null ,");
            }
            if (model.END_TIME != null)
            {
                strSql.Append("END_TIME='" + model.END_TIME + "',");
            }
            else
            {
                strSql.Append("END_TIME= null ,");
            }
            if (model.REAL_TIME != null)
            {
                strSql.Append("REAL_TIME='" + model.REAL_TIME + "',");
            }
            else
            {
                strSql.Append("REAL_TIME= null ,");
            }
            if (model.REMARK != null)
            {
                strSql.Append("REMARK='" + model.REMARK + "',");
            }
            else
            {
                strSql.Append("REMARK= null ,");
            }
            if (model.APPLICANT_TIME != null)
            {
                strSql.Append("APPLICANT_TIME='" + model.APPLICANT_TIME + "',");
            }
            else
            {
                strSql.Append("APPLICANT_TIME= null ,");
            }
            if (model.APPROVER != null)
            {
                strSql.Append("APPROVER=" + model.APPROVER + ",");
            }
            else
            {
                strSql.Append("APPROVER= null ,");
            }
            if (model.APPROVER_NAME != null)
            {
                strSql.Append("APPROVER_NAME='" + model.APPROVER_NAME + "',");
            }
            else
            {
                strSql.Append("APPROVER_NAME= null ,");
            }
            if (model.ORDER_ID != null)
            {
                strSql.Append("ORDER_ID=" + model.ORDER_ID + ",");
            }
            else
            {
                strSql.Append("ORDER_ID= null ,");
            }
            if (model.BRANCH_ID != null)
            {
                strSql.Append("BRANCH_ID=" + model.BRANCH_ID + ",");
            }
            else
            {
                strSql.Append("BRANCH_ID= null ,");
            }
            if (model.NOTATION != null)
            {
                strSql.Append("NOTATION='" + model.NOTATION + "',");
            }
            else
            {
                strSql.Append("NOTATION= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID='" + model.ID + "' ");
            strArry.Add(strSql);
            #endregion

            #region  添加采购材料
            foreach (Model.Procurement.T_PUB_PURCHASEMATERIAL item in addList)
            {
                StringBuilder strSqlm = new StringBuilder();
                StringBuilder strSqlm1 = new StringBuilder();
                StringBuilder strSqlm2 = new StringBuilder();
                if (item.PURCHASE_ID != null)
                {
                    strSqlm1.Append("PURCHASE_ID,");
                    strSqlm2.Append("'" + item.PURCHASE_ID + "',");
                }
                if (item.MATERIAL_ID != null)
                {
                    strSqlm1.Append("MATERIAL_ID,");
                    strSqlm2.Append("" + item.MATERIAL_ID + ",");
                }
                if (item.MATERIAL_NAME != null)
                {
                    strSqlm1.Append("MATERIAL_NAME,");
                    strSqlm2.Append("'" + item.MATERIAL_NAME + "',");
                }
                if (item.NUM != null)
                {
                    strSqlm1.Append("NUM,");
                    strSqlm2.Append("" + item.NUM + ",");
                }
                strSqlm.Append("insert into T_PUB_PURCHASEMATERIAL(");
                strSqlm.Append(strSqlm1.ToString().Remove(strSqlm1.Length - 1));
                strSqlm.Append(")");
                strSqlm.Append(" values (");
                strSqlm.Append(strSqlm2.ToString().Remove(strSqlm2.Length - 1));
                strSqlm.Append(")");
                strSqlm.Append(";select @@IDENTITY");
                strArry.Add(strSqlm);
            }
            #endregion

            #region 编辑采购材料
            foreach (Model.Procurement.T_PUB_PURCHASEMATERIAL item in editList)
            {
                StringBuilder strSqle = new StringBuilder();
                strSqle.Append("update T_PUB_PURCHASEMATERIAL set ");
                if (item.PURCHASE_ID != null)
                {
                    strSqle.Append("PURCHASE_ID='" + item.PURCHASE_ID + "',");
                }
                else
                {
                    strSqle.Append("PURCHASE_ID= null ,");
                }
                if (item.MATERIAL_ID != null)
                {
                    strSqle.Append("MATERIAL_ID=" + item.MATERIAL_ID + ",");
                }
                else
                {
                    strSqle.Append("MATERIAL_ID= null ,");
                }
                if (item.MATERIAL_NAME != null)
                {
                    strSqle.Append("MATERIAL_NAME='" + item.MATERIAL_NAME + "',");
                }
                else
                {
                    strSqle.Append("MATERIAL_NAME= null ,");
                }
                if (item.NUM != null)
                {
                    strSqle.Append("NUM=" + item.NUM + ",");
                }
                else
                {
                    strSqle.Append("NUM= null ,");
                }
                int m = strSqle.ToString().LastIndexOf(",");
                strSqle.Remove(m, 1);
                strSqle.Append(" where ID=" + item.ID + "");
                strArry.Add(strSqle);
            }
            #endregion

            DBHelper.ExecuteSqlTran(strArry);
        }

        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.Procurement.T_PUB_PURCHASEMATERIAL model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_PURCHASEMATERIAL(");
            strSql.Append("PURCHASE_ID,MATERIAL_ID,MATERIAL_NAME,NUM)");

            strSql.Append(" values (");
            strSql.Append("@PURCHASE_ID,@MATERIAL_ID,@MATERIAL_NAME,@NUM)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PURCHASE_ID", DbType.String, model.PURCHASE_ID);
            db.AddInParameter(dbCommand, "MATERIAL_ID", DbType.Int32, model.MATERIAL_ID);
            db.AddInParameter(dbCommand, "MATERIAL_NAME", DbType.String, model.MATERIAL_NAME);
            db.AddInParameter(dbCommand, "NUM", DbType.Double, model.NUM);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Procurement.T_PUB_PURCHASEMATERIAL model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_PURCHASEMATERIAL set ");
            strSql.Append("PURCHASE_ID=@PURCHASE_ID,");
            strSql.Append("MATERIAL_ID=@MATERIAL_ID,");
            strSql.Append("MATERIAL_NAME=@MATERIAL_NAME,");
            strSql.Append("NUM=@NUM");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, model.ID);
            db.AddInParameter(dbCommand, "PURCHASE_ID", DbType.String, model.PURCHASE_ID);
            db.AddInParameter(dbCommand, "MATERIAL_ID", DbType.Int32, model.MATERIAL_ID);
            db.AddInParameter(dbCommand, "MATERIAL_NAME", DbType.String, model.MATERIAL_NAME);
            db.AddInParameter(dbCommand, "NUM", DbType.Double, model.NUM);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_PURCHASEMATERIAL ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Procurement.T_PUB_PURCHASEMATERIAL GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PURCHASE_ID,MATERIAL_ID,MATERIAL_NAME,NUM from T_PUB_PURCHASEMATERIAL ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            Model.Procurement.T_PUB_PURCHASEMATERIAL model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PURCHASE_ID,MATERIAL_ID,MATERIAL_NAME,NUM ");
            strSql.Append(" FROM T_PUB_PURCHASEMATERIAL ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.Procurement.T_PUB_PURCHASEMATERIAL> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PURCHASE_ID,MATERIAL_ID,MATERIAL_NAME,NUM ");
            strSql.Append(" FROM T_PUB_PURCHASEMATERIAL ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.Procurement.T_PUB_PURCHASEMATERIAL> list = new List<Model.Procurement.T_PUB_PURCHASEMATERIAL>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.Procurement.T_PUB_PURCHASEMATERIAL ReaderBind(IDataReader dataReader)
        {
            Model.Procurement.T_PUB_PURCHASEMATERIAL model = new Model.Procurement.T_PUB_PURCHASEMATERIAL();
            object ojb;
            ojb = dataReader["ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ID = (int)ojb;
            }
            model.PURCHASE_ID = dataReader["PURCHASE_ID"].ToString();
            ojb = dataReader["MATERIAL_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MATERIAL_ID = (int)ojb;
            }
            model.MATERIAL_NAME = dataReader["MATERIAL_NAME"].ToString();
            ojb = dataReader["NUM"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.NUM = (decimal)ojb;
            }
            return model;
        }

        #endregion  Method
    }
}
